
const axios = require('axios')
const FormData = require('form-data')

axios.interceptors.request.use(config => {
  if (config.data instanceof FormData) {
    Object.assign(config.headers, config.data.getHeaders())
  }
  return config
})
function getForm(body={}) { 
  const form = new FormData()
  form.append('api_key', 'kXR759RZX5JhHY3tNmM3iBh4THS5keEw')
  form.append('api_secret', 'RSAPHwTaseiXv5kvhtdNp1rwaAaztpA9')
  Object.keys(body).forEach(item => { 
    form.append(item,body[item])
  })
  return form
}
class KSFace { 
  // 创建人脸库
  async faceCreateLibrary(body) { 
    try {
      const url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/create'
      const form = getForm(body)
      const result = await axios.post(url,form)
      return result.data
    } catch (error) {
      console.log('faceCreateLibrary',error)
    }
  }
  // 查看人脸库
  async faceGetLibrarys() { 
    try {
      const form = getForm()
      const url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/getfacesets'
      const result = await axios.post(url, form)
      return result.data
    } catch (error) {
      console.log('faceGetLibrarys',error)
    }
  }
  // 人脸检测
  async faceDetect(body) { 
    try {
      const form = getForm(body)
      const url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'
      const result = await axios.post(url, form)
      return result.data
    } catch (error) {
      console.log('faceDetect',error)
    }
  }
  // 添加人脸
  async faceAddface(body) { 
    try {
      const form = getForm(body)
      const url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/addface'
      const result = await axios.post(url, form)
      return result.data
    } catch (error) {
      console.log('faceAddface',error)
    }
  }
  // 获取人脸库信息-获取用户列表
  async faceFacesetDetail(body) {
    try {
      const form = getForm(body)
      const url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/getdetail'
      const result = await axios.post(url, form)
      return result.data
    } catch (error) {
      console.log('faceFacesetDetail',error)
    }
  }
  // 人脸搜索
  async faceSearch(body) { 
    try {
      const form = getForm(body)
      const url = 'https://api-cn.faceplusplus.com/facepp/v3/search'
      const result = await axios.post(url, form)
      return result.data
    } catch (error) {
      console.log('faceSearch',error)
    }
  }
}



module.exports = new KSFace()